<?php
function db_connect(){
  try{
	$user = "root";
	$pass = "root";
	$db = "concertationdb";
	
	$err = "";

	$connection = @mysql_pconnect('localhost', $user, $pass);
	
//	mysql_query("SET CHARACTER SET 'utf8'");
//	mysql_query("SET SESSION collation_connection ='utf8_general_ci'"); 
	
	if(!$connection){
	  return "Error Connection";
	}
	if(!mysql_select_db($db, $connection)){
	  return "Error Selection";
	}
	return  "1"; // Connected
  }catch(Exception $e){
	throw new Exception("db_connect:". $e->getMessage());
  }
}

function ExecuteNonQuery($sql){
	try{
		//db_connect();
		$result = mysql_query($sql);
        $arrResult = array($result, mysql_affected_rows(), mysql_error());
        return $arrResult;
	}catch(Exception $e){
		throw new Exception("ExecuteNonQuery:". $e->getMessage());
	}
}

function ExecuteReader($sql){ // Select/Query
  try{
	//db_connect();
	$result = mysql_query($sql);
	if(!$result) return "0"; // if no result

	// Convert DB to Array
	$arrTable = array();
	while($row = mysql_fetch_assoc($result)){
	  array_push($arrTable,$row);
	}
	return $arrTable;
  }catch(Exception $e){
	throw new Exception("ExecuteReader:". $e->getMessage());
  }
}

function ExecuteReaderTitleValue($sql){ // Select/Query
  try{
	//db_connect();
	$result = mysql_query($sql);
	if(!$result) return "0"; // if no result

	// Convert DB to Array
	$arrTable = array();
	while($row = mysql_fetch_assoc($result)){
	  $row=array($row[0]=>$row[0]);
	  array_push($arrTable,$row);
	}
	return $arrTable;

	//$ret = "TTTT";
	//return new soapval('status', 'xsd:string', $ret);
  }catch(Exception $e){
	throw new Exception("ExecuteReaderTitleValue:". $e->getMessage());
  }
}
function getRefURL(){
	try{
		$ref=@$HTTP_REFERER;
        $ref=$_SERVER['HTTP_CLIENT_IP'];
        $ref=$_SERVER['REMOTE_ADDR'];
        $ref=$_SERVER['HTTP_REFERER']; // Referer

		return $ref;
	}catch(Exception $e){
		throw new Exception("getPPTime:". $e->getMessage());
	}
}

function getClientIP() {
	try{
        $ip="";
        if (getenv("HTTP_CLIENT_IP"))
            $ip = getenv("HTTP_CLIENT_IP");
        else if(getenv("HTTP_X_FORWARDED_FOR"))
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        else if(getenv("REMOTE_ADDR"))
            $ip = getenv("REMOTE_ADDR");
        else
            $ip = "UNKNOWN";

        return $ip;
	}catch(Exception $e){
		throw new Exception("getPPTime:". $e->getMessage());
	}
}

function getPPTime(){
	try{
		$dateTime = new DateTime("now", new DateTimeZone('Asia/Phnom_Penh'));
		return $dateTime->format("Y-m-d H:i:s");
	}catch(Exception $e){
		throw new Exception("getPPTime:". $e->getMessage());
	}
}

function formatDateTime($strDateTime){
	try{
		$strDateTime=trim($strDateTime);

		if(empty($strDateTime)){
			return "NULL";
		}
		else{
			$strDateTime = new DateTime($strDateTime);
			$strDateTime=date_format($strDateTime, 'Y-m-d H:i:s');
			$strDateTime="'".$strDateTime."'";
	        return $strDateTime;
		}
	}catch(Exception $e){
		throw new Exception("formatDateTime:". $e->getMessage());
	}
}

function formatDate($strDateTime){
	try{
		$strDateTime=trim($strDateTime);
		if(empty($strDateTime)){
			return "NULL";
		}
		else{
			$strDateTime = new DateTime($strDateTime);
			$strDateTime=date_format($strDateTime, 'Y-m-d');
			$strDateTime = "'".$strDateTime."'";
	        return $strDateTime;
		}
	}catch(Exception $e){
		throw new Exception("formatDateTime:". $e->getMessage());
	}
}

function formatTime($strTime){
	try{
		$strTime=trim($strTime);

		if(empty($strTime)){
			return "NULL";
		}
		else{
			$strTime = new DateTime($strTime);
			$strTime=date_format($strTime, 'H:i:s');
			$strTime="'".$strTime."'";
	        return $strTime;
		}
	}catch(Exception $e){
		throw new Exception("formatTime:". $e->getMessage());
	}
}

function VEmptyString($strString){
	try{
        $strString=strval($strString);
		$strString=trim($strString);
		$strString=empty($strString)? "NULL" : "'".mysql_real_escape_string($strString)."'";
        return $strString;
	}catch(Exception $e){
		throw new Exception("VEmptyString:". $e->getMessage());
	}
}

function VEmptyStringNotNull($strString){ // for Not Null Field, for use with update function
	try{
		$strString=trim($strString);
		$strString=empty($strString)? "''" : "'".mysql_real_escape_string($strString)."'";
        return $strString;
	}catch(Exception $e){
		throw new Exception("VEmptyStringNotNull:". $e->getMessage());
	}
}

function PrepareSearchString($strString){
	try{
		$strString=trim($strString);
		$strString=str_replace(" ", "%", $strString);
        return $strString;
	}catch(Exception $e){
		throw new Exception("PrepareSearchString:". $e->getMessage());
	}
}

function StringRight($string,$count) {
$string = substr($string, -$count, $count);
return $string;
}

function getBaseUrl(){
	$currentPath = $_SERVER['PHP_SELF'];
	$pathInfo = pathinfo($currentPath);
	$hostName = $_SERVER['HTTP_HOST'];
	$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://';
	return $protocol.$hostName.$pathInfo['dirname'];
}
function getBaseUrlShot(){
	$hostName = $_SERVER['HTTP_HOST'];
	$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://';
	return $protocol.$hostName;
}
// -----------------------------------------------------------------------------------------

function testdbconnectionT(){
    //return "12";
	$result = db_connect();
	return $result;
}


?>